﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace DownloadFiles
{
    public partial class AddFileForm_CDN : DownloadFiles.AddFileForm
    {
        public AddFileForm_CDN()
        {
            InitializeComponent();
            txtPath.Text = SelectPath;
            folderBrowserDialog1.SelectedPath = SelectPath;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                SelectPath = txtPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        protected Regex urlPathRegex;
        private string path;
        private void btnOk_Click(object sender, EventArgs e)
        {
            path = txtPath.Text.Trim();
            var urls = txtUrls.Text.Trim();
            if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(urls))
            {
                MessageBox.Show("输入框不能为空！", "提示");
                return;
            }
            var list = new List<KeyValuePair<string, string>>();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            SelectPath = path;
            urlPathRegex = new Regex(txtUrlPathRegex.Text);
            foreach (var s in urls.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!IsUrl(s))
                {
                    MessageBox.Show(s + "不是url地址");
                    return;
                }
                var match = urlPathRegex.Match(s);
                if (!match.Success)
                {
                    continue;
                }
                var savePath = Path.Combine(path, match.Groups[1].Value.Replace("/", "\\"));
                list.Add(new KeyValuePair<string, string>(s, savePath));
                list.AddRange(GetVersion(s));
            }
            AddFileComplete(list);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void btnCheckVersion_Click(object sender, EventArgs e)
        {

        }

        private List<KeyValuePair<string, string>> GetVersion(string s)
        {
            var list = new List<KeyValuePair<string, string>>();
            var regex = new Regex(txtVersionRegex.Text);
            var versionList = txtVersionList.Text.Trim()
                .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var v in versionList)
            {
                var tmp = regex.Replace(s, v.Trim());
                var match = urlPathRegex.Match(tmp);
                if (!match.Success)
                {
                    continue;
                }
                var savePath = Path.Combine(path, match.Groups[1].Value.Replace("/", "\\"));
                list.Add(new KeyValuePair<string, string>(tmp, savePath));
            }
            return list;
        }
    }
}
